<?php

namespace app\admin\services;

class AmapService
{
    protected static $key='f30a3511a6ecb0be2463d6a79913e226';

    /**
     * 根据地址获取经纬度坐标
     * @param $address 地址
     * @return mixed
     */
    public static function getCoordinateByStr($address){
        $url='https://restapi.amap.com/v3/geocode/geo';
        $data=[
            'key'=>self::$key,
            'address'=>$address
        ];
        return json_decode(get($url.'?'.http_build_query($data)));
    }

    /**
     * 根据地址获取经纬度，增加逻辑判断
     * @param $address 地址
     * @return false|string[]|void
     */
    public static function getCoordinateByStrLogic($address){
        $res=self::getCoordinateByStr($address);
        if($res->status){
            $location=explode(',',$res->geocodes[0]->location);
            return $location;
        }else{
            api_return(500,$res->info);
        }
    }
}
